package com.study.chapter05.operator.sink;

import com.study.entity.Event;
import com.study.chapter05.source.ClickSource;
import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSink;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: LiuQun
 * @Date: 2022/7/30 15:30
 */
public class StreamFileSinkTest {
    public static void main(String[] args) throws Exception {
        //环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(4);

        DataStreamSource<Event> stream = env.addSource(new ClickSource()).setParallelism(1);

        //fileSink
        StreamingFileSink<String> fileSink = StreamingFileSink.<String>forRowFormat(
                new Path("./output"), //文件的输出路径
                new SimpleStringEncoder<>("UTF-8")) //编码
                .withRollingPolicy(
                        DefaultRollingPolicy.builder()
                                .withMaxPartSize(1024 * 1024 * 1024) //设置每个文件的大小，单位为字节
                                .withRolloverInterval(TimeUnit.MINUTES.toMillis(15L)) //设置每个文件多少分钟就滚动一次
                                .withInactivityInterval(TimeUnit.MINUTES.toMillis(5L)) //设置文件多久不活跃就滚动一次
                                .build()
                )
                .build();
        //将数据写入文件
        DataStreamSink<String> streamSink = stream.map(Event::toString).addSink(fileSink);


        // stream.print();

        env.execute();
    }
}
